/**
 * @(#)Calendar_Methods.java
 *
 *
 * @author
 * @version 1.00 2074/6/15
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.UIManager;
import java.util.*;
import javax.sound.sampled.*;
import javax.sound.sampled.LineEvent.Type;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Calendar_Methods {

    public Calendar_Methods() {

    }

    public static String getNotes(String date){
		BufferedReader br = null;
		String notes= null;
		try{
			String sCurrentLine;
			br = new BufferedReader(new FileReader("notes.cnf"));
			while((sCurrentLine = br.readLine()) != null){
				if(sCurrentLine.startsWith(date))
			    {
					String details[] = sCurrentLine.split("/");
					notes = details[3];
			    }

			}
		}catch(IOException ex){
			ex.printStackTrace();
		}
		return notes;
    }

    public void saveNotes(String date, String str){
    	String strCheck = str.replaceAll(" ","");
    	boolean dateCheck = false;
		BufferedReader br = null;
		BufferedWriter bw = null;
		try{
			String sCurrentLine;
			bw = new BufferedWriter(new FileWriter("temp_notes.cnf"));
			br = new BufferedReader(new FileReader("notes.cnf"));
			while((sCurrentLine = br.readLine()) != null){
				 String trimmedLine = sCurrentLine.trim();
			    if(!trimmedLine.startsWith(date))
			    {
			        // if current line not start with lineToRemove then write to file
			       	bw.write(sCurrentLine,0,sCurrentLine.length());
					bw.newLine();
			    }else{
			    	dateCheck = true;
			    }
			}
			if(strCheck.length() != 0){
				String str2 = date + "/" + str;
				bw.write(str2,0,str2.length());
				bw.newLine();
				bw.close();
				copyAFile("temp_notes.cnf","notes.cnf");
				JOptionPane.showMessageDialog(null, "The notes for " + date + " saved.");
			}else{
				if(dateCheck == true){
					bw.close();
					copyAFile("temp_notes.cnf","notes.cnf");
					JOptionPane.showMessageDialog(null, "The notes for " + date + " saved.");
				}
			}

		}catch(IOException e){
			e.printStackTrace();
		};
    }

    public static void setAlarm(String hour,String min,String sec,String alarmHourType,String alarmWav){
		String str = hour+ ":" + min + ":" + sec + ":" + alarmHourType;
		writeToAFile("alarm.cef",str);
		writeToAFile("alarmStatus.cef","On");
		writeToAFile("alarmWav.cef", alarmWav);
    }

	public static void removeAlarm(){
		writeToAFile("alarmStatus.cef","Off");
	}

	public static String[] checkAlarm(){
		BufferedReader br = null;
		String[] alarm = new String[6];
		try{
			String sCurrentLine;
			br = new BufferedReader(new FileReader("alarm.cef"));
			while((sCurrentLine = br.readLine()) != null){
				String details[] = sCurrentLine.split(":");
				alarm[0] = details[0];
				alarm[1] = details[1];
				alarm[2] = details[2];
				alarm[5] = details[3];
			}
			br = new BufferedReader(new FileReader("alarmStatus.cef"));
			alarm[3] = br.readLine();
			alarm[4] = getAlarmClip();
		}catch(IOException ex){
			ex.printStackTrace();
		}
		return alarm;
    }

	public static void triggerAlarm(){

		try{
			File fileW = new File(getAlarmClip().concat(".wav"));
			playSound();
			JOptionPane.showMessageDialog(null, "Alarm Clock");
			Days d = new Days();
			String h2 = Integer.toString(d.hour.getSelectedIndex());
		    String m2 = Integer.toString(d.min.getSelectedIndex());
		    String s2 = Integer.toString(d.sec.getSelectedIndex());
		    String[] alarmHourType = checkAlarm();
            if(d.hour.getSelectedIndex() < 10){
            	h2 = "0" + h2;
            }
            if(d.min.getSelectedIndex() < 10){
            	m2 = "0" + m2;
            }
            if(d.sec.getSelectedIndex() < 10){
            	s2 = "0" + s2;
            }
			setAlarm(h2,m2,s2,alarmHourType[5],getAlarmClip());
		}catch(Exception e){
		}

	}

	public void stopTriggeredAlarm(){

	}

	public static void writeToAFile(String dest, String str){
		BufferedWriter bw = null;
		try{
			bw = new BufferedWriter(new FileWriter(dest));
			bw.write(str,0,str.length());
			bw.close();
		}catch(IOException e){
			e.printStackTrace();
		};
	}

	public static void copyAFile(String src, String dest){
		BufferedReader br = null;
		BufferedWriter bw = null;
		try{
			String sCurrentLine;
			br = new BufferedReader(new FileReader(src));
			bw = new BufferedWriter(new FileWriter(dest));
			while((sCurrentLine = br.readLine()) != null){
				bw.write(sCurrentLine);
				bw.newLine();
			}
			bw.close();
		}catch(IOException ex){
			ex.printStackTrace();
		}
	}

	public static void playSound() {
        try {
        	File f1 = new File(getAlarmClip().concat(".wav"));
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f1);
            Clip clip = AudioSystem.getClip( );
            clip.open(audioInputStream);
            clip.start( );
        }
        catch(Exception e)  {
            e.printStackTrace( );
        }
    }


	public boolean checkIfAlarm(String time){
		BufferedReader br = null;
		boolean needToTrigger = false;
		try{
			String sCurrentLine;
			br = new BufferedReader(new FileReader("alarmStatus.cef"));
			while((sCurrentLine = br.readLine()) != null){
				if(sCurrentLine.equals("On")){
					needToTrigger = true;
				}
			}
			if(needToTrigger == true){
				br = new BufferedReader(new FileReader("alarm.cef"));
				while((sCurrentLine = br.readLine()) != null){
					if(sCurrentLine.equals(time)){
						needToTrigger = true;
					}else{
						needToTrigger = false;
					}
				}
			}

		}catch(IOException ex){
			ex.printStackTrace();
		}
		return needToTrigger;
	}
	
	public static String getAlarmClip() {
                BufferedReader br = null;
                String clip = null;
                try{
                        String sCurrentLine;
                        br = new BufferedReader(new FileReader("alarmWav.cef"));
                        while((sCurrentLine = br.readLine()) != null) {
                                if(sCurrentLine.startsWith("alarm")) {
                                        clip = sCurrentLine;
                                }
                        }
                }catch(IOException ex) {
                        ex.printStackTrace();
                }
                return clip;
        }
}